cmake_minimum_required(VERSION 3.12.4)

set(FFMPEG_VERSION "3.4.12")
set(FFMPEG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ffmpeg-${FFMPEG_VERSION})
set(FFMPEG_INSTALL_DIR ${CMAKE_INTERNAL_INTERNAL_PREFIX})

# 解压FFmpeg源码
if(NOT EXISTS ${FFMPEG_SOURCE_DIR})
    execute_process(
        COMMAND ${CMAKE_COMMAND} -E tar xf ${CMAKE_CURRENT_SOURCE_DIR}/ffmpeg-${FFMPEG_VERSION}.tar.xz
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    )
endif()

# 配置FFmpeg编译选项
set(FFMPEG_CONFIGURE_OPTIONS
    --prefix=${FFMPEG_INSTALL_DIR}
    --enable-cross-compile
    --cross-prefix=${CROSS_PREFIX}
    --sysroot=${CROSS_SYSROOT}
    --cc=${CMAKE_C_COMPILER}
    --cxx=${CMAKE_CXX_COMPILER}
    --strip=${CROSS_PREFIX}strip
    --arch=arm
    --target-os=linux
    --enable-shared
    --enable-static
    --disable-doc
    --disable-ffplay
    --disable-ffprobe
    --disable-ffserver
    --disable-avdevice
    --disable-jack
    --disable-bzlib
    --disable-doc
    --disable-htmlpages
    --disable-manpages
    --disable-podpages
    --disable-txtpages
    --enable-gpl
    --enable-nonfree
    --enable-version3
    --enable-small
    --enable-zlib
    --enable-decoder=h264,mpeg4,aac,mp3,png,mjpeg
    --enable-parser=h264,aac,mpegaudio,png
    --enable-demuxer=mov,mp4,matroska,mpegts,image2
    --enable-protocol=file
    --enable-swscale
    --extra-cflags="-I${CROSS_COMPILE_INCLUDE_PATH}"
)

message("ffmpeg configure params: ${FFMPEG_CONFIGURE_OPTIONS}")

# 修改configure目标后，需要删除 build/ffmpeg-build/config.stamp 文件强制重新配置
# 添加configure目标
add_custom_target(ffmpeg_configure
    COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/ffmpeg-build
    COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/ffmpeg-build/config.stamp
    COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_CURRENT_BINARY_DIR}/ffmpeg-build
            ${FFMPEG_SOURCE_DIR}/configure ${FFMPEG_CONFIGURE_OPTIONS}
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    COMMENT "Configuring FFmpeg ${FFMPEG_VERSION}"
)

# 添加build目标，只在config.stamp不存在时执行configure
add_custom_target(ffmpeg_build
    COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_CURRENT_BINARY_DIR}/ffmpeg-build
            [ -f config.stamp ] || ${CMAKE_MAKE_PROGRAM} ffmpeg_configure
    COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_CURRENT_BINARY_DIR}/ffmpeg-build
            $(MAKE)
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    COMMENT "Building FFmpeg ${FFMPEG_VERSION}"
)

# 添加install目标
add_custom_target(ffmpeg_install
    COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_CURRENT_BINARY_DIR}/ffmpeg-build
            $(MAKE) install
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    COMMENT "Installing FFmpeg ${FFMPEG_VERSION}"
)

# 设置目标依赖关系
#add_dependencies(ffmpeg_build ffmpeg_configure)
add_dependencies(ffmpeg_install ffmpeg_build)

# 添加一个总目标，依赖于所有步骤
add_custom_target(ffmpeg ALL
    DEPENDS ffmpeg_install
    COMMENT "Complete FFmpeg build process"
)

install(DIRECTORY ${FFMPEG_INSTALL_DIR}/lib/
    DESTINATION lib
    FILES_MATCHING PATTERN "*.so*")

install(DIRECTORY ${FFMPEG_INSTALL_DIR}/bin/
    DESTINATION bin)